package top.zackyoung.demo.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import top.zackyoung.demo.conf.ConfEntity;
import top.zackyoung.demo.entity.Result;
import top.zackyoung.demo.entity.Student;
import top.zackyoung.demo.entity.Teacher;

import java.util.ArrayList;
import java.util.List;

/**
 * @author: ZackYoung
 * @time: 2020/9/13
 */
@Controller
public class DemoController {
    @RequestMapping("/demo1")
    public String demo1(){
        return "demo1";
    }

    @Autowired
    Person person;
    /**
     * 显示个人信息
     */
    @RequestMapping("/personInfo")
    @ResponseBody
    public Person personInfo(){
        return person;
    }

    @Autowired
    ConfEntity confEntity;

    @RequestMapping("/chOrStut")
    @ResponseBody
    public Result[] tchOrStu(){
        //模拟从数据库查出老师的信息
        Teacher teacher1=new Teacher(1,"张老师",33,"java");
        Teacher teacher2=new Teacher(1,"张老师",33,"c++");
        //模拟从数据库查出老师的信息
        Student student1=new  Student(1,"张大大","软一");
        Student student2=new  Student(1,"李小小","软二");

        List<Teacher> teachers= new ArrayList<>();
        teachers.add(teacher1);
        teachers.add(teacher2);

        List<Student> students = new ArrayList<>();
        students.add(student1);
        students.add(student2);

        Result<Teacher> teacherResult = new Result<>();
        Result<Student > studentResult = new Result<>();
        Result[] result= {studentResult, teacherResult};


        for (String param : confEntity.getParams()) {
            if ( "teacher".equals(param)){
                teacherResult.setT(teachers);
            }
            else if ( "student".equals(param)){
                studentResult.setT(students);
            }else if ("".equals(param)){
                teacherResult.setCode(3);
                teacherResult.setMsg("不存在");
            }
        }

        return result;
    }
}